package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemCarHallVideoBinding;
import com.sgb.kjwl.databinding.ItemEnterpriseZhuanyetuanduiBinding;
import com.sgb.kjwl.model.entity.enterprise.TeamChangeEntity;
import com.swgk.core.base.model.entity.VideoBean;
import com.sgb.kjwl.model.entity.transport.SingleItemConstant;
import com.sgb.knowledgeshare.view.activity.PlayListActivity;
import com.sgb.kjwl.view.ui.adapter.transport.GoodsInfoVideoAdapter;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class HomeTeamAdapter extends BaseMultiItemQuickAdapter<TeamChangeEntity, BaseViewHolder> {
    public HomeTeamAdapter(List<TeamChangeEntity> datas) {
        super(datas);
        addItemType(SingleItemConstant.TYPE_ONE, R.layout.item_enterprise_zhuanyetuandui);
        //视频类别
        addItemType(SingleItemConstant.TYPE_TWO, R.layout.item_enterprise_video_list);
        //单个视频类别
        addItemType(SingleItemConstant.TYPE_THREE, R.layout.item_enterprise_single_video);
    }

    @Override
    protected void convert(BaseViewHolder helper, TeamChangeEntity item) {
        switch (item.getItemType()) {
            case SingleItemConstant.TYPE_ONE:
                setTeam(helper, item);
                break;
            case SingleItemConstant.TYPE_TWO:
                initVideoList(helper, item);
                break;
            case SingleItemConstant.TYPE_THREE:
                initSingleVideo(helper, item);
                break;
        }

    }

    private void setTeam(BaseViewHolder holder, TeamChangeEntity teamChangeEntity) {
        ItemEnterpriseZhuanyetuanduiBinding binding = DataBindingUtil.bind(holder.itemView);
        binding.setViewModel(teamChangeEntity);
        binding.setAdapter(iCallPhone);
    }

    /**
     * 设置视频信息
     */
    private void initVideoList(BaseViewHolder helper, TeamChangeEntity entity) {
        List<VideoBean> videoBeanList = new ArrayList<>();
        videoBeanList.addAll(entity.getVideos());
        RecyclerView recyclerView = helper.getView(R.id.list_data);
        if (entity.getVideos() == null || entity.getVideos().size() == 0) {
            recyclerView.setVisibility(View.GONE);
            return;
        }
        recyclerView.setVisibility(View.VISIBLE);
        if (recyclerView.getAdapter() == null) {
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            GoodsInfoVideoAdapter adapter = new GoodsInfoVideoAdapter(mContext, videoBeanList);
            adapter.setOnItemClick(new AdapterOnItemClick<VideoBean>() {
                @Override
                public void onItemClick(VideoBean homeHostVideoEntity, int position) {
                    Intent intent = new Intent(mContext, PlayListActivity.class);
                    intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) adapter.getDatas());
                    intent.putExtra(PlayListActivity.PSO, position);
                    intent.putExtra(PlayListActivity.VIDEOTYPE, "goods_and_car_video");
                    mContext.startActivity(intent);
                }
            });
            recyclerView.setAdapter(adapter);
        } else {
            ((GoodsInfoVideoAdapter) recyclerView.getAdapter()).setDatas(videoBeanList);
        }
    }

    /**
     * 设置单个视频信息
     */
    private void initSingleVideo(BaseViewHolder helper, TeamChangeEntity entity) {
        //设置dataBinding数据
        ItemCarHallVideoBinding binding = DataBindingUtil.bind(helper.itemView);
        if (binding == null) return;
        if (entity.getVideos() != null && !entity.getVideos().isEmpty()) {
            binding.setVariable(BR.viewModel, entity.getVideos().get(0));
            binding.executePendingBindings();
        }
    }

    private ICallPhone iCallPhone;

    public interface ICallPhone {
        void call(String num, String name);
    }

    public void OnCallPhone(ICallPhone iCallPhone) {
        this.iCallPhone = iCallPhone;
    }
}
